package com.ruoyi.web.controller.app;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WcChangePatentApplication;
import com.ruoyi.system.domain.WcChangePatentApplicationAncillary;
import com.ruoyi.system.domain.vo.business.WcChangePatentApplicationDetailVO;
import com.ruoyi.system.domain.vo.business.WcChangePatentApplicationSaveVO;
import com.ruoyi.system.domain.vo.business.WcChangePatentApplicationSelectListDto;
import com.ruoyi.system.domain.vo.business.WcChangePatentApplicationSelectListVO;
import com.ruoyi.system.service.IWcChangePatentApplicationAncillaryService;
import com.ruoyi.system.service.IWcChangePatentApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.List;

/**
 * 剧本变更备案申请Controller
 *
 * @author ruoyi
 * @date 2024-02-28
 */
@Controller
@Api(tags = {"(app)剧本备案变更申请"})
@RequestMapping("/app/system/wcChangePatentApplication")
public class AppWcChangePatentApplicationController extends BaseController {

    @Autowired
    private IWcChangePatentApplicationService wcChangePatentApplicationService;

    @Autowired
    private IWcChangePatentApplicationAncillaryService ancillaryService;

    @Autowired
    private IWcChangePatentApplicationService service;
    /**
     * 新增保存剧本变更备案申请
     */
    @ApiOperation("(app)新增剧本变更备案申请")
    //@RequiresPermissions("system:application:add")
    @Log(title = "(app)剧本变更备案申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody WcChangePatentApplicationSaveVO saveVO) {
        try {
            wcChangePatentApplicationService.addOrUpdateSave(saveVO,saveVO.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return success();
    }

    /**
     * 修改保存剧本变更备案申请
     */
    @ApiOperation("(app)修改剧本变更备案申请")
    //@RequiresPermissions("system:application:add")
    @Log(title = "(app)修改剧本变更备案申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody WcChangePatentApplicationSaveVO saveVO) {
        try {
            wcChangePatentApplicationService.addOrUpdateSave(saveVO,null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return success();
    }


    /**
     * 修改保存剧本变更备案申请
     */
    @ApiOperation("(app)修改剧本变更备案申请")
    //@RequiresPermissions("system:application:add")
    @Log(title = "(app)修改剧本变更备案申请", businessType = BusinessType.INSERT)
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult updateSave(@RequestBody WcChangePatentApplicationSaveVO saveVO) {
        WcChangePatentApplication entity = new WcChangePatentApplication();
        BeanUtils.copyBeanProp(entity, saveVO);
        entity.setUpdateTime(DateUtils.getNowLocalDateTime());
        entity.setUpdateUser(getUserId());
        wcChangePatentApplicationService.updateById(entity);

        WcChangePatentApplicationAncillary ancillary = new WcChangePatentApplicationAncillary();
        BeanUtils.copyBeanProp(ancillary, saveVO);
        ancillary.setWcChangePatentApplicationId(entity.getId());
        ancillaryService.updateById(ancillary);

        return success();
    }

    /**
     * (app)剧本变更备案申请详情
     */
    @ApiOperation("(app)剧本变更备案申请详情")
    //@RequiresPermissions("system:application:edit")
    @Log(title = "(app)剧本变更备案申请详情", businessType = BusinessType.UPDATE)
    @GetMapping("/detail/{id}")
    @ResponseBody
    public AjaxResult<WcChangePatentApplicationSaveVO> detail(@PathVariable("id") Long id) {
        WcChangePatentApplicationSaveVO vo = wcChangePatentApplicationService.selectDetailVOById(id);
        return success(vo);
    }

    @ApiOperation("删除剧本变更备案申请")
    //@RequiresPermissions("system:application:remove")
    @Log(title = "剧本变更备案申请", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id) {
        boolean b = service.removeById(id);
        //附件一块删除
        ancillaryService.remove(Wrappers.lambdaQuery(WcChangePatentApplicationAncillary.class).
                eq(WcChangePatentApplicationAncillary::getWcChangePatentApplicationId,id));
        return toAjax(b);
    }

}
